'use strict'
const path = require('path')
const defaultSettings = require('./src/settings.js')
const version = new Date().getTime()
function resolve(dir) {
  return path.join(__dirname, dir)
}

const name = defaultSettings.title || 'DMS Admin' // 页面标题

// If your port is set to 80,
// use administrator privileges to execute the command line.
// For example, Mac: sudo npm run
// You can change the port by the following methods:
// port = 9528 npm run dev OR npm run dev --port = 9528
const port = process.env.port || process.env.npm_config_port || 9528 // dev port

// server module ip&port
const lyf = port => `http://10.186.90.2:${port}`
const fzb = port => `http://10.186.90.81:${port}`
const yh = port => `http://10.186.90.98:${port}`
const lth = port => `http://10.186.90.11:${port}`
const wn = port => `http://10.186.90.11:${port}`
const js = port => `http://10.186.90.7:${port}`
// 整车销售
// const vehiclesale_url = yh(10001)
const vehiclesale_url = 'http://172.26.239.170:31200'
// 系统管理
// const sysmgr_url = yh(10002)
const sysmgr_url = 'http://172.26.239.170:31200'
// 售后管理
// const aftersale_url = js(10003)
const aftersale_url = 'http://172.26.239.170:31200'
// 备件管理
// const spareparts_url = yh(10005)
const spareparts_url = 'http://172.26.239.170:31200'  

const isProd = process.env.NODE_ENV === 'production'

// All configuration item explanations can be find in https://cli.vuejs.org/config/
module.exports = {
  /**
   * You will need to set publicPath if you plan to deploy your site under a sub path,
   * for example GitHub Pages. If you plan to deploy your site to https://foo.github.io/bar/,
   * then publicPath should be set to "/bar/".
   * In most cases please use '/' !!!
   * Detail: https://cli.vuejs.org/config/#publicpath
   */
  publicPath: '/',
  outputDir: 'dist',
  assetsDir: 'static',
  // false去掉语法提示
  lintOnSave: false, // process.env.NODE_ENV === 'development', // development、production、test
  productionSourceMap: false,
  devServer: {
    port: port,
    open: true,
    overlay: {
      warnings: false,
      errors: true
    },
    // 去掉proxy、after即可移除Mock模拟请求 luojx 20190715
    proxy: {
      // change xxx-api/login => mock/login
      // detail: https://cli.vuejs.org/config/#devserver-proxy

      // ==================mock + 开发环境API=======================
      // Allen
      // '/dev-api/mp/e3sh/vesale': {
      //   target: `http://172.16.185.19:10001`,
      //   ws: true,
      //   changeOrigin: true,
      //   pathRewrite: {
      //     '^/dev-api': 'http://172.16.185.19:10001'
      //   },
      //   logLevel: 'debug'
      // },
      // '/dev-api/mp/e3sh/aftersale': {
      //   target: `http://172.16.185.19:10003`,
      //   ws: true,
      //   changeOrigin: true,
      //   pathRewrite: {
      //     '^/dev-api': 'http://172.16.185.19:10003'
      //   },
      //   logLevel: 'debug'
      // },
      // '/dev-api/mp/e3sh/aftersale': {
      //   target: `http://172.16.186.3:10003`,
      //   ws: true,
      //   changeOrigin: true,
      //   pathRewrite: {
      //     '^/dev-api': 'http://172.16.186.3:10003'
      //   },
      //   logLevel: 'debug'
      // },
      // '/dev-api/mp/e3sh/aftersale': {
      //   target: `http://172.16.184.44:10003`,
      //   ws: true,
      //   changeOrigin: true,
      //   pathRewrite: {
      //     '^/dev-api': 'http://172.16.184.44:10003'
      //   },
      //   logLevel: 'debug'
      // },
      // // end
      // //   // 外采供应商查询
      // '/mp/e3sh/spareparts': {
      //   target: `http://172.16.184.112:10004`,
      //   ws: true,
      //   changeOrigin: true,
      //   pathRewrite: {
      //     '^/dev-api': 'http://172.16.184.112:10004'
      //   },
      //   logLevel: 'debug'
      // },
     // 外采供应商查询
     '/mp/e3sh/aftersale': {
      target: aftersale_url,
      ws: true,
      changeOrigin: true,
      pathRewrite: {
        '^/dev-api':aftersale_url
      },
      logLevel: 'debug'
    },
    //系统门店信息
    '/dev-api/e3sh/org/': {
      target: sysmgr_url,
      ws: true,
      changeOrigin: true,
      pathRewrite: {
        '^/dev-api': sysmgr_url
      },
      logLevel: 'debug'
    },
      // 公共值列表
      '/dev-api/mp/e3sh/vesale/common': {
        target: vehiclesale_url,
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/dev-api': vehiclesale_url
        },
        logLevel: 'debug'
      },

      // 基础数据
      '/dev-api/mp/e3sh/spareparts':{
        target: spareparts_url,
        ws: true,
        changeOrigin: true,
        pathRewrite: {
        '^/dev-api': spareparts_url
      },
      logLevel: 'debug'
      },
      // 销退审核
      // '/mp/e3sh/SalesReturnManagement/ve': {
      //   target: `http://172.16.184.54:10009/`,
      //   ws: true,
      //   changeOrigin: true,
      //   pathRewrite: {
      //     '^/dev-api': 'http://172.16.184.54:10009/'
      //   },
      //   logLevel: 'debug'
      // },

      // 补贴管理-汇总表
      // '/dev-api/mp/e3sh/basics/ve/subsidySummaryService/': {
      //   target: `http://172.16.184.24:10003/`,
      //   ws: true,
      //   changeOrigin: true,
      //   pathRewrite: {
      //     '^/dev-api': 'http://172.16.184.24:10003/'
      //   },
      //   logLevel: 'debug'
      // },

      // '/dev-api/mp/e3sh/vesale/basics/smallareaservice': {
      //   target: `http://172.16.184.54:10001`,
      //   ws: true,
      //   changeOrigin: true,
      //   pathRewrite: {
      //     '^/dev-api': '172.16.184.54:10001'
      //   },
      //   logLevel: 'debug'
      // },

      '/dev-api/mp/login': {
        target: `http://172.26.239.170:31200`,
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/dev-api': 'http://172.26.239.170:31200'
        },
        logLevel: 'debug'
      },
      '/dev-api/mp/framework': {
        target: `http://172.26.239.170:31200`,
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/dev-api': 'http://172.26.239.170:31200'
        },
        logLevel: 'debug'
      },
      '/dev-api/mp/notice': {
        target: `http://172.26.239.170:31200`,
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/dev-api': 'http://172.26.239.170:31200'
        },
        logLevel: 'debug'
      },
      '/dev-api/mp/org': {
        target: `http://172.26.239.170:31200`,
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/dev-api': 'http://172.26.239.170:31200'
        },
        logLevel: 'debug'
      },
      '/dev-api/mp/file': {
        target: `http://172.26.239.170:31200`,
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/dev-api': 'http://172.26.239.170:31200'
        },
        logLevel: 'debug'
      },
      '/dev-api/mp/log': {
        target: `http://172.26.239.170:31200`,
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/dev-api': 'http://172.26.239.170:31200'
        },
        logLevel: 'debug'
      },
      '/dev-api/mp/job': {
        target: `http://172.26.239.170:31200`,
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/dev-api': 'http://172.26.239.170:31200'
        },
        logLevel: 'debug'
      },
      '/dev-api/mp/wfdesign': {
        target: `http://172.26.239.170:31200`,
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/dev-api': 'http://172.26.239.170:31200'
        },
        logLevel: 'debug'
      },
      '/dev-api/mp/wfengine': {
        target: `http://172.26.239.170:31200`,
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/dev-api': 'http://172.26.239.170:31200'
        },
        logLevel: 'debug'
      },
      '/dev-api/mp/wfmonitor': {
        target: `http://172.26.239.170:31200`,
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/dev-api': 'http://172.26.239.170:31200'
        },
        logLevel: 'debug'
      },

      // wms 开发环境api
      '/dev-api/mp/wm/vl/basedata': {
        target: `http://172.16.184.118:8801`,
        ws: true,
        changeOrigin: true,
        logLevel: 'debug'
      },
      '/dev-api/mp/wm/vl/warehouse': {
        target: `http://172.16.184.118:8802`,
        ws: true,
        changeOrigin: true,
        logLevel: 'debug'
      },

      // 本地图片
      '/upload/notice': {
        target: `http://172.16.159.78:31200`,
        // ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/dev-api': '172.16.159.78:31200'
        },
        logLevel: 'debug'
      },

	  // default
      '/dev-api': {
        target: `http://127.0.0.1:${port}/mock`,
        changeOrigin: true,
        pathRewrite: {
          '^/dev-api': 'http://127.0.0.1:${port}/mock'
        },
        logLevel: 'debug'
      }
    },
    after: require('./mock/mock-server.js')
  },
  configureWebpack: {
    output: { // 输出重构  打包编译后的 文件名称  【模块名称.版本号.时间戳】
      // 编译的时候把下面的注释打开
      filename: `static/js/[name].${isProd?'[chunkhash]':'[hash]'}.${version}.js`,      
      chunkFilename: `static/js/[name].${isProd?'[chunkhash]':'[hash]'}.${version}.js`
    }
  },
  chainWebpack(config) {
    config.plugins.delete('preload')
    config.plugins.delete('prefetch')
    config.module
      .rule('images')
      .use('url-loader')
      .tap(options => {
        options.name = `static/img/[name].${version}.[ext]`
        options.fallback = {
          loader: 'file-loader',
          options: {
            name: `static/img/[name].${version}.[ext]`
          }
        }
      return options
    })
    config.module
      .rule('svg')
      .exclude.add(resolve('src/icons'))
      .end()
    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('src/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
      .end()

    // set preserveWhitespace
    config.module
      .rule('vue')
      .use('vue-loader')
      .loader('vue-loader')
      .tap(options => {
        options.compilerOptions.preserveWhitespace = true
        return options
      })
      .end()

    config
      // https://webpack.js.org/configuration/devtool/#development
      .when(process.env.NODE_ENV === 'development',
        config => config.devtool('cheap-source-map')
      )

    config
      .when(process.env.NODE_ENV !== 'development',
        config => {
          config
            .plugin('ScriptExtHtmlWebpackPlugin')
            .after('html')
            .use('script-ext-html-webpack-plugin', [{
              // `runtime` must same as runtimeChunk name. default is `runtime`
              inline: /runtime\..*\.js$/
            }])
            .end()
          config
            .optimization.splitChunks({
              chunks: 'all',
              cacheGroups: {
                libs: {
                  name: 'chunk-libs',
                  test: /[\\/]node_modules[\\/]/,
                  priority: 10,
                  chunks: 'initial' // 只打包初始时依赖的第三方
                },
                elementUI: {
                  name: 'chunk-elementUI', // 单独将 elementUI 拆包
                  priority: 20, // 权重要大于 libs 和 app 不然会被打包进 libs 或者 app
                  test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
                },
                org: {
                  name: 'chunk-org',
                  test: resolve('src/components/org'), // can customize your rules
                  minChunks: 1, //  minimum common number
                  priority: 20,
                  reuseExistingChunk: true
                },
                ve: {
                  name: 'chunk-ve',
                  test: resolve('src/components/ve'), // can customize your rules
                  minChunks: 1, //  minimum common number
                  priority: 20,
                  reuseExistingChunk: true
                },
                pa: {
                  name: 'chunk-pa',
                  test: resolve('src/components/pa'), // can customize your rules
                  minChunks: 1, //  minimum common number
                  priority: 20,
                  reuseExistingChunk: true
                },
                se: {
                  name: 'chunk-se',
                  test: resolve('src/components/se'), // can customize your rules
                  minChunks: 1, //  minimum common number
                  priority: 20,
                  reuseExistingChunk: true
                },
                crm: {
                  name: 'chunk-crm',
                  test: resolve('src/components/crm'), // can customize your rules
                  minChunks: 1, //  minimum common number
                  priority: 20,
                  reuseExistingChunk: true
                },
                commons: {
                  name: 'chunk-commons',
                  test: resolve('src/components'), // can customize your rules
                  minChunks: 3, //  minimum common number
                  priority: 5,
                  reuseExistingChunk: true
                }
              }
            })
          config.optimization.runtimeChunk('single')
        }
      )
  }
}
